﻿
#region Namespace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class ucSearchUtility : System.Web.UI.UserControl
{
    UserManager _utilityManager = new UserManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSearchUtility.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearchUtility.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnAddUtility.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAddUtility.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnEditUtility.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEditUtility.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnGroups.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnGroups.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDeleteUtility.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDeleteUtility.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");


        btnDeleteUtility.Attributes.Add("OnClick", "return confirmDelete()");
       
        if (!IsPostBack)
        {
            Session["UserManager_UtilitySearch"] = "Latest";
            Search();
            upnlAddUtility.Visible = false;
            //upnlUpdate.Visible = false;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Search();
    }

    protected void lnbUsers_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucUserSearch.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void lnbRoles_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucRole.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void lnbUtilities_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchAddUtility.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void lnbPermissions_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucAddPermission.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void gvwUtility_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwUtilities.PageIndex = e.NewPageIndex;
        Search();
    }

    protected void btnAddGroup_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserManager/AddModifyUtilityGroup.aspx");
    }
    protected void btnModifyGroup_Click(object sender, EventArgs e)
    {
        Response.Redirect("UtilityGroup.aspx");
    }

   
    protected void btnSearchUtility_Click(object sender, EventArgs e)
    {
        if (txtSearchUtility.Text == string.Empty)
            Session["UserManager_UtilitySearch"] = "BlankSearch";
        else
            Session["UserManager_UtilitySearch"] = txtSearchUtility.Text;

        Search();   
    }

        
    protected void btnAddUtility_Click(object sender, EventArgs e)
    {
        if (ddlGroup.Items.Count==0)
            InitializeDDL();
        tblSearch.Visible = false;
        pnlUtility.Visible = false;
        upnlAddUtility.Visible = true;
        Session["UserManager_Utility"] = "Utility Add";
        btnSave.Text = "Save";
    }
    protected void btnEditUtility_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            if (ddlGroup.Items.Count == 0)
                InitializeDDL();
            tblSearch.Visible = false;
            pnlUtility.Visible = false;
            upnlAddUtility.Visible = true;
            Session["UserManager_Utility"] = "Utility Edit";
            upnlAddUtility.Visible = true;
            LoadData();
            btnSave.Text = "Update";
        }
    }
    protected void btnDeleteUtility_Click(object sender, EventArgs e)
    {
        Delete();
    }
    protected void gvwUtilities_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        tblSearch.Visible = true;
        pnlUtility.Visible = true;
        upnlAddUtility.Visible = false;
        Search();
    }
    protected void btnGroups_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtUtility.Text == string.Empty)
        {
            Response.Write("<script>alert('Enter Utility Name')</script>");
            return;
        }
        if (Session["UserManager_Utility"].ToString() == "Utility Add")
            Add();
        else
            Edit();

        Clear();
        InitializeDDL();
    }
  
    protected void gvwUtilities_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwUtilities.PageIndex = e.NewPageIndex;
        Search();
    }
    protected void gvwUtilities_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        } 
    }

    void Delete()
    {
        int index = 0;
        string utilityID;
        string[] utilityIDs = new string[gvwUtilities.Rows.Count];
        for (int i = 0; i < gvwUtilities.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUtilities.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                utilityIDs[index] = gvwUtilities.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            Response.Write("<script>alert('Select Utility')</script>");
            return;
        }
        string conditionDeletePermissionMap = "PM_UtilityID='" + utilityIDs[0] + "'";
        string conditionDeleteUtility = "UT_UtilityID='" + utilityIDs[0] + "'";

        for (int i = 1; i < index; i++)
        {
            conditionDeletePermissionMap = conditionDeletePermissionMap + "or PM_UtilityID='" + utilityIDs[i] + "'";
            conditionDeleteUtility = conditionDeleteUtility + "or UT_UtilityID='" + utilityIDs[i] + "'";
        }
        _utilityManager.DeleteUtility(conditionDeletePermissionMap, conditionDeleteUtility);
        Response.Write("<script>alert('Deleted Successfully !')</script>");
        Search();
    }

    void LoadData()
    {
        string condition = "UT_UtilityID = " + Session["UserManager_UtilityID"] + " ";
        DataSet dsUser = _utilityManager.GetData("UtilityList", condition);

        txtUtility.Text = dsUser.Tables[0].Rows[0]["UT_UtilityName"].ToString();

    }

    void Clear()
    {
        txtSearchUtility.Text = string.Empty;
        txtUtility.Text = string.Empty;
    }

    void InitializeDDL()
    {
        DataSet dsGroup = _utilityManager.GetData("UtilityGroup", string.Empty);

        ddlGroup.DataSource = dsGroup;
        ddlGroup.DataTextField = "UG_GroupName";
        ddlGroup.DataValueField = "UG_GroupID";
        ddlGroup.DataBind();
        ddlGroup.Items.Insert(0, "Select");

        //ddlGroupEdit.DataSource = dsGroup;
        //ddlGroupEdit.DataTextField = "UG_GroupName";
        //ddlGroupEdit.DataValueField = "UG_GroupID";
        //ddlGroupEdit.DataBind();
        //ddlGroupEdit.Items.Insert(0, "Select");
    }

    void Search()
    {
        try
        {
            DataSet ds = new DataSet();
            _utilityManager.SearchUtility(Session["UserManager_UtilitySearch"].ToString(), out ds);

            gvwUtilities.DataSource = ds;
            gvwUtilities.Columns[1].Visible = true;
            gvwUtilities.DataBind();
            gvwUtilities.Columns[1].Visible = false;

            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('No Utilities Found !')</script>");
            }

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Utility Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Add()
    {
        try
        {
            Utility utilityAdd = new Utility();

            utilityAdd.UtilityName = txtUtility.Text;
            utilityAdd.GroupID = string.Empty;
            if (ddlGroup.SelectedIndex != 0)
            {
                utilityAdd.GroupID = ddlGroup.SelectedValue;
            }

            if (!_utilityManager.AddUtility(utilityAdd))
            {
                Response.Write("<script>alert('Utility Already Exist')</script>");
            }
            else
                Response.Write("<script>alert('Added Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Utility Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Edit()
    {
        try
        {
            if (txtUtility.Text == string.Empty)
            {
                Response.Write("<script>alert('Enter Utility Name')</script>");
                return;
            }

            Utility utilityEdit = new Utility();
            utilityEdit.UtilityID = Session["UserManager_UtilityID"].ToString();
            utilityEdit.UtilityName = txtUtility.Text;
            if (ddlGroup.SelectedIndex != 0)
                utilityEdit.GroupID = ddlGroup.SelectedValue;
            else
                utilityEdit.GroupID = string.Empty;

            if (_utilityManager.EditUtility(utilityEdit))
                Response.Write("<script>alert('Modified Successfully!')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Utility Modification Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwUtilities.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUtilities.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            Response.Write("<script>alert('Select Utility')</script>");
            return false;
        }
        else if (count > 1)
        {
            Response.Write("<script>alert('Select Single Utility')</script>");
            return false;
        }

        Session["UserManager_UtilityID"] = gvwUtilities.Rows[selectedRow].Cells[1].Text;
        return true;
    }
}
